package org.bonorsoft.modules.controller;

import net.sf.saxon.trans.SymbolicName;
import org.bonorsoft.modules.utils.QrCodeUtil;
import org.bonorsoft.modules.utils.ZxingUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/qrcode")
public class CodeController {

    @GetMapping()
    public String index(){
        return "qrcode/qrcode";
    }

    /**
     * qrcode-plugin 工具包实现
     * @param file
     * @param text
     * @param flag
     * @return
     */
    @PostMapping("/create")
    @ResponseBody
    public String createQrCode(@RequestParam(value = "logoFile", required = false) MultipartFile file,
                               @RequestParam(value = "text") String text,
                               @RequestParam(value = "flag") String flag){
        String qrcode = "";
        try {
            switch (flag){
                case "normal":
                    qrcode = QrCodeUtil.normal(text);
                    break;
                case "logo":
                    qrcode = QrCodeUtil.logo(text, file.getInputStream());
                    break;
                case "color":
                    qrcode = QrCodeUtil.color(text);
                    break;
                case "background":
                    qrcode = QrCodeUtil.background(text, file.getInputStream());
                    break;
                case "style":
                    qrcode = QrCodeUtil.style(text);
                    break;
                case "imageFill":
                    qrcode = QrCodeUtil.imageFill(text, file.getInputStream());
                    break;
                default:
                    qrcode = QrCodeUtil.gif(text, file.getInputStream());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qrcode;
    }

    /**
     * zxing实现
     * @param file
     * @param text
     * @return
     * @throws Exception
     */
    @PostMapping("/zxing")
    @ResponseBody
    public String zxing(@RequestParam(value = "logoFile", required = false) MultipartFile file,
                        @RequestParam(value = "text") String text) throws Exception {
        if (file == null ) return ZxingUtil.createImage(text, null);
        else return ZxingUtil.createImage(text, file.getInputStream());
    }

}
